import { _decorator, Component, Node, Label } from 'cc';
import { BasicUI } from '../../Init/Basic/BasicUI';
import { GlobalConfig } from '../../Init/Config/GlobalConfig';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { AudioEnum } from '../../Init/SystemAudio/AudioEnum';
import { AudioSystem } from '../../Init/SystemAudio/AudioSystem';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { UISystem } from '../../Init/SystemUI/UISystem';
import { Adsdk } from '../../myScript/Adsdk';
const { ccclass, property } = _decorator;

@ccclass('FreeGoldUI')
export class FreeGoldUI extends BasicUI {

    @property(Label)
    protected awardLabel: Label = null;
    private _awardNum = 0;

    public show(d) {
        super.show(d);
        let clv = StorageSystem.getData().levelAssets.curLv;
        this._awardNum = Math.floor(clv * 250 + 750);
        this.awardLabel.string = this._awardNum.toFixed(0);

        EventManager.emit(EventTypes.GameEvents.GamePause);

        Adsdk.instance.hideAllAd();
        Adsdk.instance.showInterstitialAd();
        Adsdk.instance.showGridAdTop();
        Adsdk.instance.showBanner();
        Adsdk.instance.showGridAdRightOne();
        Adsdk.instance.showGridAdLeftOne();
    }

    public hide(d) {
        super.hide(d);
        EventManager.emit(EventTypes.GameEvents.GameResume);
    }

    private saveAwards() {
        StorageSystem.setData((d) => {
            d.userAssets.asset += this._awardNum;
        }, true);
        StorageSystem.updateToAssets(true);
    }

    protected onCloseClick() {
        AudioSystem.playEffect(AudioEnum.BtnClick);
        UISystem.hideUI(UIEnum.FreeGoldUI);

        Adsdk.instance.hideAllAd();
    }

    protected onVideoClick() {
        //视频成功之后
        this.emit(EventTypes.SDKEvents.ShowVideo, () => {
            AudioSystem.playEffect(AudioEnum.BtnClick);
            UISystem.hideUI(UIEnum.FreeGoldUI);
            this.saveAwards();
        })
    }

}

